<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use console\models\Files;
use common\components\Upload;

class FilesController extends Controller
{
    public function actionCleanFiles(){
        $now = time()-30*24*3600;
        $condition = ['and',
            ['<','created_at',$now],
            ['type'=>'event'],
            ['media'=>'gallery']
        ];
        $list = Files::find()->where($condition)->asArray()->all();

        foreach ($list as $file){
            $model = Files::find()->where(['id'=>$file['id']])->asArray()->one();
            $this->removeFile($model['path']);
            $this->removeFile($model['path_s']);
            echo 'delete file '.$model['path']."\n";
        }

        //删除记录
        Files::deleteAll($condition);
    }

    //删除文件
    protected function removeFile($path)
    {
        $file  = new Upload();
        $file->delFile($path);
    }
}
